package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.CompositeTemplate;
import com.soft.sitegenerator.business.hibernate.SimpleTemplate;
import com.soft.sitegenerator.business.hibernate.User;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.SimpleTemplateEditForm;

public class SimpleTemplateEditAction extends DispatchAction {

	public ActionForward editSimpleTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SimpleTemplateEditForm simpleTemplateEditForm = (SimpleTemplateEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		SimpleTemplate simpleTemplate = simpleTemplateEditForm
				.getSimpleTemplate();
		simpleTemplate.setId(id);

		try {
			simpleTemplate.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		simpleTemplateEditForm.setUsers(user.findAll());
		simpleTemplateEditForm.setIdOwner(simpleTemplate.getOwner().getId());

		CompositeTemplate compositeTemplate = (CompositeTemplate) component.getBean("compositeTemplatesHib");
		simpleTemplateEditForm.setCompositeTemplates(compositeTemplate.findAll());
		if (simpleTemplate.getCompositeTemplate() != null)
			simpleTemplateEditForm.setIdCompositeTemplate(simpleTemplate.getCompositeTemplate().getId());

		return mapping.findForward("showEdit");
	}

	public ActionForward deleteSimpleTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SimpleTemplateEditForm simpleTemplateEditForm = (SimpleTemplateEditForm) form;

		Long id = new Long(Long.parseLong(request.getParameter("id")));

		simpleTemplateEditForm.getSimpleTemplate().setId(id);
		simpleTemplateEditForm.getSimpleTemplate().delete();

		return mapping.findForward("showList");
	}

	public ActionForward addSimpleTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {

		SimpleTemplateEditForm simpleTemplateEditForm = (SimpleTemplateEditForm) form;

		Component component = new DataBaseComponent();
		User user = (User) component.getBean("usersHib");
		simpleTemplateEditForm.setUsers(user.findAll());

		CompositeTemplate compositeTemplate = (CompositeTemplate) component.getBean("compositeTemplatesHib");
		simpleTemplateEditForm.setCompositeTemplates(compositeTemplate.findAll());

		return mapping.findForward("showAdd");
	}

	public ActionForward saveSimpleTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SimpleTemplateEditForm simpleTemplateEditForm = (SimpleTemplateEditForm) form;

		Component component = new DataBaseComponent();
		CompositeTemplate compositeTemplate = null;
		if (simpleTemplateEditForm.getIdCompositeTemplate().intValue() != 0) {
			compositeTemplate = (CompositeTemplate) component.getBean("compositeTemplatesHib");
			compositeTemplate.setId(simpleTemplateEditForm.getIdCompositeTemplate());

			try {
				compositeTemplate.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		User owner = (User) component.getBean("usersHib");
		owner.setId(simpleTemplateEditForm.getIdOwner());
		try {
			owner.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		SimpleTemplate simpleTemplate = simpleTemplateEditForm.getSimpleTemplate();
		simpleTemplate.setCompositeTemplate(compositeTemplate);
		simpleTemplate.setOwner(owner);
		if (simpleTemplate.getCanUpdate() == null)
			simpleTemplate.setCanUpdate("no");
		simpleTemplate.save();

		return mapping.findForward("showList");

	}

	public ActionForward updateSimpleTemplate(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		SimpleTemplateEditForm simpleTemplateEditForm = (SimpleTemplateEditForm) form;

		Component component = new DataBaseComponent();
		CompositeTemplate compositeTemplate = null;
		if (simpleTemplateEditForm.getIdCompositeTemplate().intValue() != 0) {
			compositeTemplate = (CompositeTemplate) component.getBean("compositeTemplatesHib");
			compositeTemplate.setId(simpleTemplateEditForm.getIdCompositeTemplate());

			try {
				compositeTemplate.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		User owner = (User) component.getBean("usersHib");
		owner.setId(simpleTemplateEditForm.getIdOwner());
		try {
			owner.load();
		} catch (ObjectNotFoundException e) {
			e.printStackTrace();
		}

		SimpleTemplate simpleTemplate = simpleTemplateEditForm.getSimpleTemplate();
		simpleTemplate.setCompositeTemplate(compositeTemplate);
		simpleTemplate.setOwner(owner);
		if (simpleTemplate.getCanUpdate() == null)
			simpleTemplate.setCanUpdate("no");
		simpleTemplate.update();

		return mapping.findForward("showList");

	}

}